package to_do

import (
	"context"
	"github.com/golang/protobuf/proto"
	"github.com/micro/go-micro/v2/metadata"
	"kop/pb"
)

// IDs[0]: roleID, IDs[1]: serverID, IDs[2]: guildID, IDs[3]: userID,
func NewUser(IDs ...int32) pb.User {

	var user = pb.User{RoleID: IDs[0]}
	if len(IDs) > 1 {
		user.ServerID = IDs[1]
	}
	if len(IDs) > 2 {
		user.GuildID = IDs[2]
	}
	if len(IDs) > 3 {
		user.UserID = IDs[3]
	}

	return user
}

func GetUser(ctx context.Context) (pb.User, error) {

	var user pb.User
	var m, _ = metadata.FromContext(ctx)

	return user, proto.Unmarshal([]byte(m[PbUser]), &user)
}

func WithUser(ctx context.Context, user pb.User) context.Context {

	return context.WithValue(ctx, PbUser, user)
}
